<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/10/29
 * Time: 17:50
 */
namespace App\Http\Controllers\Home\HomeList;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeListController extends BaseController
{
    /**
     * 前台所有页面请求分发模板...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $list
     */
    public function index($list)
    {
        if(empty($list)) abort(404);
        //根据数据库模板调用机制调用模板
        $route = '/list/'.$list;
        $menu = DB::table('menulist')
                    ->where('status',1)
                    ->where('route',$route)
                    ->select('id','templates_type','title','banner_img','banner_url')
                    ->first();
        if(!empty($menu->templates_type))
        {
            switch($menu->templates_type)
            {
                case 1:
                    return self::defaultInTemplates($menu->id,$menu->banner_img,$menu->banner_url);
                    break;
                case 2:
                    return self::defaultListTem($menu->id,$menu->title,$route,$menu->banner_img,$menu->banner_url);
                    break;
                case 3:
                    return self::defaultNewListTem($menu->id,$menu->title,$route,$menu->banner_img,$menu->banner_url);
                    break;
            }
        }else{
            abort(404);
        }
    }

    /**
     * 默认显示内页...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $menuID
     */
    public static function defaultInTemplates($menuID,$banner_img,$banner_url)
    {
        if(empty($menuID)) abort(404);
        $data = array();
        $data['con'] = DB::table('article')
                            ->where(['menu_id'=>$menuID,'status'=>1])
                            ->select('content')
                            ->first();
        $data['banner_img'] = $banner_img;
        $data['banner_url'] = $banner_url;
        return view('Home.defaultInTemplates.index')->withData($data);
    }

    /**
     * 默认显示列表页...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $menuID
     */
    public static function defaultListTem($menuID,$title,$route,$banner_img,$banner_url)
    {
        $data = self::getPageData($menuID,$title,$route);
        $data['banner_img'] = $banner_img;
        $data['banner_url'] = $banner_url;
        return view('Home.defaultListTem.index')->withData($data);
    }

    /**
     * 显示新闻列表页...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $menuID
     */
    public static function defaultNewListTem($menuID,$title,$route,$banner_img,$banner_url)
    {
        $data = self::getPageData($menuID,$title,$route);
        $data['mouth'] = array(
            1   =>  'January',
            2   =>  'February',
            3   =>  'March',
            4   =>  'April',
            5   =>  'May',
            6   =>  'June',
            7   =>  'July',
            8   =>  'August',
            9   =>  'September',
            10  =>  'Qctober',
            11  =>  'November',
            12  =>  'December'
        );
        $data['banner_img'] = $banner_img;
        $data['banner_url'] = $banner_url;
        return view('Home.defaultNewListTem.index')->withData($data);
    }

    /**
     * 公共列表页拉去数据...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $menuID
     * @param $title
     * @param $route
     * @return mixed
     */
    public static function getPageData($menuID,$title,$route)
    {
        if(empty($menuID)) abort(404);

        $data['title']  = $title;
        $data['pageNum'] = 6;
        //文章列表数据,带标签搜索页搜索数据
        $data['list'] = DB::table('article')
            ->where(['status'=>1,'menu_id'=>$menuID]);
        $data['list'] = !empty($_GET['tag_id']) ? $data['list']->join('tag_article',function($join){
            $join->on('tag_article.article_id','=','article.id')
                ->where('tag_article.tag_id','=',$_GET['tag_id']);
        }) : $data['list'];
        $data['list'] = $data['list']->orderBy('article.create_time','desc')
            ->select('article.id','article.title','article.description','article.create_time','article.covers_img')
            ->paginate($data['pageNum']);
        $data['route'] = $route;
        //推荐文章拉取
        $data['recommend'] = DB::table('article')
            ->where(['status'=>1,'menu_id'=>$menuID,'is_rec'=>2])
            ->orderBy('create_time','desc')
            ->select('id','description','create_time','covers_img')
            ->limit(3)
            ->get();
        //拉去热门标签
        $data['hotTag'] = DB::table('tag')
            ->where(['status'=>1,'menu_id'=>$menuID])
            ->orderBy('create_time','desc')
            ->select('tag_name','id')
            ->get();
        return $data;
    }

    /**
     * 默认文章页...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $id
     * @return mixed
     */
    public function article($id)
    {
        if(empty($id)) abort(404);
        $data = array();
        $data['con'] = DB::table('article')
            ->where(['id'=>$id,'status'=>1])
            ->select('content')
            ->first();
        return view('Home.defaultInTemplates.index')->withData($data);
    }

    /**
     * 新闻文章页...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $id
     * @return mixed
     */
    public function newarticle($id)
    {
        if(empty($id)) abort(404);
        $data = array();
        //获取文章数据
        $data['con'] = DB::table('article')
            ->where(['id'=>$id,'status'=>1])
            ->select('content','create_time','title','author_from','click_num','menu_id')
            ->first();
        //获取推荐新闻
        $data['recommend'] = DB::table('article')
            ->where(['status'=>1,'menu_id'=>$data['con']->menu_id,'is_rec'=>2])
            ->orderBy('create_time','desc')
            ->select('id','description','create_time','covers_img')
            ->limit(3)
            ->get();
        //热门标签
        $data['hotTag'] = DB::table('tag')
            ->where(['status'=>1,'menu_id'=>$data['con']->menu_id])
            ->orderBy('create_time','desc')
            ->select('tag_name','id')
            ->get();
        //获取上一页下一页
        $data['prev'] = DB::table('article')
            ->where(['status'=>1,'menu_id'=>$data['con']->menu_id])
            ->where('id','<',$id)
            ->select('id')
            ->orderBy('id','desc')
            ->first();
        $data['next'] = DB::table('article')
            ->where(['status'=>1,'menu_id'=>$data['con']->menu_id])
            ->where('id','>',$id)
            ->select('id')
            ->orderBy('id','desc')
            ->first();
        //阅读数更新
        DB::update('update wj_article set click_num = click_num+1 where id = ?',[$id]);
        return view('Home.newArticle.index')->withData($data);
    }
}